import React from 'react';
import { connect } from 'dva';
import { Link, browserHistory } from 'dva/router';
import { Table, Row, Col, Form, Input, Radio, Select, Button, message, Popconfirm } from 'antd';
const R = require('ramda');
import RightBox from '../../../components/commons/RightBox';
import RightConstants from '../../../utils/RightConstants';
import Constants from '../../../utils/Constants';
import VipInvoiceRecord from '../../../components/vip/VipInvoiceRecord';
const FormItem = Form.Item;
export default connect(({ vipManage }) => ({ vipManage }))(
    Form.create()(class invoiceRecord extends React.Component {
        constructor(props) {
            super(props);
        }

        componentDidMount() {
            this.props.dispatch({ type: 'vipManage/queryInvoiceRecords' });//开票信息查询
        }

        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.vipManage.invoPage;
            newPage.page = 1;
            this.props.dispatch({
                type: 'vipManage/queryInvoiceRecords',
                payload: {
                    cstName: values.cstName,
                    page: newPage
                }
            });
        };

        collectSearchCondition = () => {
            let conditions = this.props.form.getFieldsValue();
            conditions.page = this.props.vipManage.invoPage;
            return conditions;
        }

        handleView = (record) => {
            this.props.dispatch({ type: 'vipManage/getInvoiceRecordById', payload: { id: record.id } });//获取开票信息详情
            this.vipInvoiceRecord.handleOpen(true, true, this.collectSearchCondition(), record);
        }

        handleDelete = (record) => {
            this.props.dispatch({ type: 'vipManage/deleteInvoiceRecords', payload: { id: record.id, searchConditions: this.collectSearchCondition() } });//删除开票信息
        }

        handleEdit = (record, disabled, subDisabled) => {
            this.props.dispatch({ type: 'vipManage/getInvoiceRecordById', payload: { id: record.id } });//编辑开票信息
            this.vipInvoiceRecord.handleOpen(true, subDisabled, this.collectSearchCondition(), record);
        }

        columns = [
            { title: '客户名称', dataIndex: 'cstName', key: 'cstName' },
            { title: '发票类型', dataIndex: 'invoiceType', key: 'invoiceType',
                render: (text, record, index) => {
                    return (Constants.VIP_CUSTOMER_INVOICETYPE[record.invoiceType])
            }
        },
            { title: '开票金额', dataIndex: 'inoviceAmt', key: 'inoviceAmt' },
            { title: '纳税人识别号', dataIndex: 'taxesnum', key: 'taxesnum' },
            { title: '联系电话', dataIndex: 'telephone', key: 'telephone' },
            { title: '开户银行', dataIndex: 'openingbank', key: 'openingbank' },
            { title: '银行账号', dataIndex: 'bankaccount', key: 'bankaccount' },
            { title: '详细地址', dataIndex: 'address', key: 'address' },
            { title: '状态', dataIndex: 'status', key:'status',
                render: (text, record, index) => {
                    return (Constants.VIP_CUSTOMER_INVOICE_STATUS[record.status])
            }
        },
            {
                title: '操作', key: 'oprater',
                render: (text, record, index) => {                   
                    let deleteLink =
                    <Popconfirm onConfirm={this.handleDelete.bind(this, record)} okText="是" cancelText="否" placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'}>
                        <a href="#">删除</a>
                    </Popconfirm>
                    deleteLink = <RightBox resource={RightConstants.VIP.VIP_MANAGE_INVOICE_DELETE} split={true}>{deleteLink}</RightBox>;                    
                    let editLink = <a href="#" onClick={this.handleEdit.bind(this, record, false, false)}>编辑</a>;
                    editLink = record.status === "2" ? null: <RightBox resource={RightConstants.VIP.VIP_MANAGE_INVOICE_UPDATE} split={true}>{editLink}</RightBox>;
                    let viewLink = <a href="#" onClick={this.handleView.bind(this, record)}>查看</a>
                    return (<span>{editLink}{deleteLink}{viewLink}</span>);
                }
            }
        ];

        render() {
            let dataSource = R.isNil(this.props.vipManage.invoList) ? {} : this.props.vipManage.invoList;
            let formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                current: this.props.vipManage.invoPage.page,
                pageSize: this.props.vipManage.invoPage.size,
                total: this.props.vipManage.invoPage.totalRow,
                showSizeChanger: true,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.vipManage.invoPage;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'vipManage/queryInvoiceRecords',
                        payload: {
                            cstName: values.cstName,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.vipManage.invoPage;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'vipManage/queryInvoiceRecords',
                        payload: {
                            cstName: values.cstName,
                            page: newPage
                        }
                    });
                }
            }

            return (
                <div>
                    <Row>
                        <Col span={6}><FormItem {...formItemLayout} label={'客户名称'}>{getFieldDecorator('cstName')(<Input />)}</FormItem></Col>
                        <Col span={6} style={{ float: 'right' }}>
                            <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>
                    </Row>
                    <Table rowKey={record => record.id} dataSource={dataSource} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                    <VipInvoiceRecord wrappedComponentRef={(inst) => this.vipInvoiceRecord = inst} dataSource={this.props.vipManage.dataSource} dispatch={this.props.dispatch} />
                </div>
            )
        }
    })
)

